package k.server;

import java.io.File;
import java.util.List;

import k.db.DBConfig;
import k.util.XmlUtil;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class RealTimeLog2DB {

	// private static int threadNum  =  1 ;
	
	private static int sleepMillisecond = 1000;
	
	
	public static void initLoger()
	{
		
		
		String log4jconfigFilePath = System.getProperty("user.dir") + File.separator
				+ "config" + File.separator + "log4j.properties";	
		PropertyConfigurator.configure(log4jconfigFilePath);
		
		// DOMConfigurator.configure("config.xml");
	}
	
	public static DBConfig readDBConfig()
	{
		
		String dbConfigFilePath = System.getProperty("user.dir") + File.separator
				+ "config" + File.separator + "RealTimeLog2DB.xml";	
		return XmlUtil.ReadDBConfig(dbConfigFilePath);
	}
	
	public static List<LogConfig> readLogConfig()
	{
		String dbConfigFilePath = System.getProperty("user.dir") + File.separator
				+ "config" + File.separator + "RealTimeLog2DB.xml";	
		return XmlUtil.ReadLogConfig(dbConfigFilePath);
	}
 	
	
	
	
	public static void main(String[] args) {

		initLoger();
		Logger logger = Logger.getLogger(RealTimeLog2DB.class.getName());
		
		if(logger == null)
		{
			return;
		}
		logger.info("log init ok");
		
		DBConfig dbconfig= readDBConfig();
		if(dbconfig ==null)
		{
			logger.error("read db config error");
			return;
		}
		
		logger.info("dbconfig init ok");
		List<LogConfig>  logConfigList = readLogConfig();
		if(logConfigList == null  || logConfigList.isEmpty())
		{
			logger.error("read log config error");
			return;
		}
		
		
		final Worker worker =  new Worker(); 
		
		Runtime.getRuntime().addShutdownHook(new Thread(){

			@Override
			public void run() {
				
				worker.stop_wait();
			}
			
		});
		
		worker.setDbconfig(dbconfig);	
		worker.setSleepTime(sleepMillisecond);
		worker.setLogConfigList(logConfigList);

		
		Thread thread = new Thread(worker);
		thread.start();

	}

}
